﻿using Mysqlx.Crud;
using Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using xiaoxiong.KYJDAL.Models;

namespace xiaoxiong.KYJDAL.Services
{
    public class SysLogService :  Repository<SysLog>
    {
        //添加Log日志
        //添加操作日志
        //添加报警日志
        public async Task<bool> AddSysLog(SysLog sysLog)
        {
             return await base.InsertAsync(sysLog);
        }


        //查出所有
        public async Task<List<SysLog>> GetSysLogList()
        {
            return await base.GetListAsync();
        }

        //分页查询 一次展示出15条
        public async Task<List<SysLog>> GetSysLogList(int skip, int take)
        {
            var list = await base.Context.Queryable<SysLog>()
                            .OrderByDescending(l => l.InsertTime)
                            .Skip(skip)
                            .Take(take)
                            .ToListAsync();


            // 计算全局起始序号
            int startSN = skip + 1;

            for (int i = 0; i < list.Count; i++)
            {
                list[i].SN = startSN + i; // 当前记录在全局的序号
            }
            return list;
        }

        public async Task<List<SysLog>> GetSysLogTimeCount(DateTime startTime, DateTime endTime,String logType)
        {
            // 直接通过数据库查询过滤数据
            // 构建查询条件
            var query = base.Context.Queryable<SysLog>()
                .Where(e => SqlFunc.ToDate(e.InsertTime) >= startTime
                         && SqlFunc.ToDate(e.InsertTime) <= endTime);
            // 如果 logType 不为空或 null，则添加过滤条件
            if (!string.IsNullOrEmpty(logType))
            {
                query = query.Where(e => e.LogType == logType);
            }
            // 执行查询
            var list = await query
                .OrderByDescending(e => SqlFunc.ToDate(e.InsertTime))
                .ToListAsync();
            return list;
        }

        // 条件分页查询
        public async Task<(List<SysLog> Data, int TotalCount)> GetSysLogTimePaged(
            DateTime startTime,
            DateTime endTime,
            string logType,
            int pageIndex,
            int pageSize)
        {
            RefAsync<int> totalCount = 0;
            var query = base.Context.Queryable<SysLog>()
                .Where(e => SqlFunc.ToDate(e.InsertTime) >= startTime
                         && SqlFunc.ToDate(e.InsertTime) <= endTime);
            if (!string.IsNullOrEmpty(logType))
            {
                query = query.Where(e => e.LogType == logType);
            }
            var list = await query
                .OrderByDescending(e => SqlFunc.ToDate(e.InsertTime))
                .ToPageListAsync(pageIndex, pageSize, totalCount);
            // 计算SN码（当前页的起始序号）
            int startSN = (pageIndex - 1) * pageSize + 1;
            for (int i = 0; i < list.Count; i++)
            {
                list[i].SN = startSN + i;
            }
            return (list, totalCount);
        }


        public async Task<List<SysLog>> GetAllConditionData(DateTime? startTime = null,
                                                   DateTime? endTime = null,
                                                   string logType = "")
        {
            var query = base.Context.Queryable<SysLog>();

            // 添加时间条件
            if (startTime != null && endTime != null)
            {
                query = query.Where(e =>
                    SqlFunc.ToDate(e.InsertTime) >= startTime.Value &&
                    SqlFunc.ToDate(e.InsertTime) <= endTime.Value);
            }

            // 添加日志类型条件
            if (!string.IsNullOrEmpty(logType))
            {
                query = query.Where(e => e.LogType == logType);
            }

            // 获取数据并排序
            var list = await query
                .OrderByDescending(e => SqlFunc.ToDate(e.InsertTime))
                .ToListAsync();

            // 生成全局连续SN（从1开始）
            for (int i = 0; i < list.Count; i++)
            {
                list[i].SN = i + 1;
            }

            return list;
        }

    }
}
